# @shuaige : 陈世玉
# @name :03-绕过验证码登录.py
# @time :2024/12/6 16:27
import requests
url='https://www.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fwww.gushiwen.cn%2fuser%2fcollect.aspx'
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'
}
responce=requests.get(url,headers=headers)
from bs4 import BeautifulSoup
soup=BeautifulSoup(responce.text,'lxml')
image=soup.select('#imgCode')[0].attrs.get('src')
image_url='http://so.gushiwen.cn'+image
print(image_url)
viewstate=soup.select('#__VIEWSTATE')[0].attrs.get('value')
viewstategenerator=soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')
# print(viewstate)
# print(viewstategenerator)
session=requests.session()
image_data=session.get(image_url).content
#wb模式就是将二进制文件写进文件
with open('img.jpg','wb') as f:
    f.write(image_data)
    # print('验证码已保存')
    # f.close()
code_name=input('请输入验证码: ')
data={
    '__VIEWSTATE':viewstate,
    '__VIEWSTATEGENERATOR':viewstategenerator,
    'from': 'http://www.gushiwen.cn/user/collect.aspx',
    'email': '17550826672',
    'pwd': 'admin123',
    'code': code_name,
    'denglu': '登录'
}
# session
responce_post=session.post(url,data=data,headers=headers)
print(responce_post.text)